<?php

use Biz\model\salesRebate\SalesCompany as mSalesCompany;
use Biz\service\salesRebate\SalesRebate as sSalesRebate;

class CompanyAccountAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REALNAME;

    public function main()
    {
        $uid = $this->uid;
        $result = [];
        if ($add = $this->getRequest()->getPost('add', '')) {
            $add = json_decode($add, true);
            $time = date('Y-m-d H:i:s');
            $save = [
                'uid' => $uid,
                'name' => $add['name'],
                'bank_no' => $add['bank_no'],
                'bank_name' => $add['bank_name'],
                'province' => $add['province'],
                'city' => $add['city'],
                'area' => $add['area'],
                'subbranch' => $add['subbranch']
            ];
            $result = $save;
            //修改和新增
            if (isset($add['id'])) {
                //编辑
                $where = ['id' => $add['id']];
                $save['edittime'] = $time;
                $result['id'] = $add['id'];
                try {
                    mSalesCompany::update($save, $where);
                    $this->setSuccess($result);
                } catch (\Exception $e) {
                    throw new \Exception('修改失败');
                }
            } else {
                $isValid = sSalesRebate::isVaild($uid);
                if ($isValid) {
                    //新增
                    $save['addtime'] = $time;
                    $run = mSalesCompany::save($save);
                    if ($run) {
                        $result['id'] = $run;
                        $this->setSuccess($result);
                    } else {
                        throw new \Exception('创建失败');
                    }
                }
            }
        }
    }

}